using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.BusinessEntities.Enums;
using WCSFContrib.ECommerceCatalog.Admin.Services;
using WCSFContrib.ECommerceCatalog.CatalogData;
using System.Web.Security;

namespace WCSFContrib.ECommerceCatalog.Admin.Tests.Services
{
    /// <summary>
    /// Summary description for UsersProfilesServiceFixture
    /// </summary>
    [TestClass]
    public class UsersProfilesServiceFixture
    {
        private CatalogDataSet ds;
        private UsersProfilesService usersProfilesService;

        [TestInitialize()]
        public void MyTestInitialize()
        {
            ds = new CatalogDataSet();
            ds.Address.AddAddressRow("One Microsoft Way", "Redmond", "Seattle", "United States", "5555-5555");
            ds.Address.AddAddressRow("47th Av", "Vancouver", "Washington", "United States", "5555-4444");
            ds.Address.AddAddressRow("Madison Street", "Chicago", "Illinois", "United States", "5555-3333");

            ds.PaymentType.AddPaymentTypeRow(1, "CreditCard");
            ds.PaymentType.AddPaymentTypeRow(2, "BankAccount");
            ds.PaymentType.AddPaymentTypeRow(3, "Checks");

            ds.Payment.AddPaymentRow(3, "No info - Checks");
            ds.Payment.AddPaymentRow(1, "No info - Credit Card");
            ds.Payment.AddPaymentRow(2, "No info - Bank Account");

            ds.UserProfile.AddUserProfileRow("jdoe");
            ds.UserProfile.AddUserProfileRow("alincoln");

            ds.UserProfile_Address.AddUserProfile_AddressRow(ds.UserProfile[0], ds.Address[0]);
            ds.UserProfile_Address.AddUserProfile_AddressRow(ds.UserProfile[0], ds.Address[1]);
            ds.UserProfile_Address.AddUserProfile_AddressRow(ds.UserProfile[1], ds.Address[2]);

            ds.UserProfile_Payment.AddUserProfile_PaymentRow(ds.UserProfile[0], ds.Payment[0]);
            ds.UserProfile_Payment.AddUserProfile_PaymentRow(ds.UserProfile[1], ds.Payment[1]);
            ds.UserProfile_Payment.AddUserProfile_PaymentRow(ds.UserProfile[1], ds.Payment[2]);

            ds.AcceptChanges();

            usersProfilesService = new UsersProfilesService(ds);
        }

        [TestMethod]
        public void ShouldGetUserProfileById()
        {
            UserProfile userProfile = usersProfilesService.GetUserProfileById(1);
            Assert.AreEqual<int>(1, userProfile.UserProfileId);
            Assert.AreEqual<int>(2, userProfile.Addresses.Count);
            Assert.AreEqual<int>(1, userProfile.PaymentsMethods.Count);
        }

        [TestMethod]
        public void ShouldGetAllUsersProfiles()
        {
            ICollection<UserProfile> allUsersProfiles = usersProfilesService.AllUsersProfiles;
            Assert.IsNotNull(allUsersProfiles);
            Assert.AreEqual<int>(2, allUsersProfiles.Count);
        }

        [TestMethod]
        public void ShouldGetUserProfileByUserName()
        {
            UserProfile userProfile = usersProfilesService.GetUserProfileByUserName("jdoe");

            Assert.IsNotNull(userProfile);
            Assert.AreEqual<int>(1, userProfile.UserProfileId);
        }

        [TestMethod]
        public void ShouldGetUserProfileByUserNameReturnsNullWithNoMatch()
        {
            UserProfile userProfile = usersProfilesService.GetUserProfileByUserName("No Match");

            Assert.IsNull(userProfile);
        }

        [TestMethod]
        public void ShouldAddAddress()
        {
            Address address = new Address(1, "One Microsoft Street", "Redmond", "Washington", "United States", "5555-5555");
            
            usersProfilesService.AddAddress(1, address);
            UserProfile userProfile = usersProfilesService.GetUserProfileById(1);

            Assert.AreEqual<int>(3, userProfile.Addresses.Count);
            Assert.AreEqual<string>("Redmond", userProfile.Addresses[2].City);
        }

        [TestMethod]
        public void ShouldRemoveAddress()
        {
            usersProfilesService.RemoveAddress(1, 3);
            usersProfilesService.RemoveAddress(1, 2);
            UserProfile userProfile = usersProfilesService.GetUserProfileById(1);

            Assert.AreEqual<int>(1, userProfile.Addresses.Count);
            Assert.AreEqual<string>("Redmond", userProfile.Addresses[0].City);
        }

        [TestMethod]
        public void ShouldAddPaymentMethod()
        {
            Payment payment = new Payment(1, PaymentType.Checks, "more info");
            
            usersProfilesService.AddPaymentMethod(2, payment);
            UserProfile userProfile = usersProfilesService.GetUserProfileById(2);

            Assert.AreEqual<int>(3, userProfile.PaymentsMethods.Count);
            Assert.AreEqual(PaymentType.Checks, userProfile.PaymentsMethods[2].PaymentType);
        }

        [TestMethod]
        public void ShouldRemovePaymentMethod()
        {
            usersProfilesService.RemovePaymentMethod(2, 1);
            usersProfilesService.RemovePaymentMethod(2, 2);
            usersProfilesService.RemovePaymentMethod(2, 3);

            UserProfile userProfile = usersProfilesService.GetUserProfileById(2);

            Assert.AreEqual<int>(0, userProfile.PaymentsMethods.Count);
        }
    }
}
